/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schoolLink;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Style;

/**
 *
 * @author Admin
 */
public class LoginForm implements ActionListener{
    private SchoolLink parent;
    private Form f;
    private TextField textName;
    private TextField textPass;
    private Button buttonOk;
    
    public LoginForm(SchoolLink parent){
        this.parent = parent;
    }
         
    public void initForm() {

//        Resources res;
//        try {
//            res = Resources.open("/LWUITtheme.res");
//            //UIManager.getInstance().setThemeProps(res.getTheme("myTheme"));
//            UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }
        
//        try {
//            //open the resource file
//            //get and set the theme
//            Resources r = Resources.open("/LWUITtheme1.res");
//            UIManager.getInstance().
//                    setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
//        } catch (java.io.IOException e) {
//            //if there is a problem print a message on console
//            //in this case default settings will be used
//            System.out.println("Unable to get Theme " + e.getMessage());
//        }    
        
        f = new Form();
        f.setTitle(parent.appNAme);
        f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        //set form background colour
        //f.getStyle().setBgColor(0xd5fff9);
        
        Label labelName= new Label("Tên đăng nhập");
        Label labelPass= new Label("Mật khẩu");
        textName = new TextField("123", 20);
        textName.setText(parent.userName);
        
        textPass = new TextField("", 20);
        textPass.setText(parent.password);
        textPass.setConstraint(TextField.PASSWORD);
        
//        textPass.setEditable(true);
//        textName.setEditable(true);
//        textName.setFocusable(true);        
//        //textPass.setFocusable(true);
//        textName.requestFocus();
        
        
//        int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
//        textName.keyPressed(i);
//        textName.keyReleased(i);
    
        //TextField textName = new TextField("123", 4);
        //TextField textPass = new TextField("123", 4);
        //f.addComponent("Center", new Label("I am a Label"));
        //BoxLayout boxLayout = new BoxLayout(BoxLayout.Y_AXIS);

        //f.addComponent(boxLayout, textName);
        f.addComponent(labelName);
        f.addComponent(textName);
        f.addComponent(labelPass);
        f.addComponent(textPass);
        // 4. Set Transitions animation of Fade
        f.setTransitionOutAnimator(CommonTransitions.createFade(400));
        // 5. Add Command key
        Command exitCmd = new Command("Thoát", 1) {
             public void actionPerformed(ActionEvent evt) {
                //showMessage( Constant.STRING_APP_CONFIRM_EXIT );
                //System.out.println("!!Thoát "   + evt);
                Dialog dialog = new Dialog(parent.appNAme);
                dialog.setLayout(new BorderLayout());
                Command cmdOk = new Command("Đồng ý", 1)
                {
                    public void actionPerformed(ActionEvent evt) {
                        parent.destroyApp(true);
                    }
                };
                Command cmdCancel = new Command("Hủy", 2)
                {
                    public void actionPerformed(ActionEvent evt) {
                    }
                };
                //dialog.addComponent(new Label(Constant.STRING_APP_CONFIRM_EXIT)); 
                TextArea textArea = null;
                textArea = new TextArea(Constant.STRING_APP_CONFIRM_EXIT);
                textArea.setSingleLineTextArea(false);
                textArea.setGrowByContent(true);
                textArea.setFocusable(false);
                dialog.addComponent(BorderLayout.CENTER,textArea);
                
                dialog.addCommand(cmdOk);
                dialog.addCommand(cmdCancel);
                dialog.show(90,90,10,10,true);
            }
        };
        Command loginCmd = new Command("Đăng nhập", 2) {
            public void actionPerformed(ActionEvent evt) {
                //System.out.println("!!!!! Đăng nhập: "   + evt);
                //eventFormInit();
                if (textName.getText().length()<=0 || textPass.getText().length()<=0){
                    
                }else{
                    String[] s = {(textName.getText().trim()),(textPass.getText().trim())};
                    parent.initForConnect(Constant.ACTION_LOGIN_LOGIN, s);
                }
            }
        };      
        //f.setCyclicFocus(false);
        f.setFocused(textName);
        f.addCommand(exitCmd);
        f.addCommand(loginCmd);

        //f.addComponent(BorderLayout.SOUTH, textPass);
        f.show();
    }
    public void showMessage(String mess){
        Dialog dialogMessage = null;
        dialogMessage = new Dialog();
        dialogMessage.getTitleStyle().setBgTransparency(0);
        dialogMessage.getDialogStyle().setBgTransparency(0);
        dialogMessage.setLayout(new BorderLayout());

        TextArea textArea = null;
        textArea = new TextArea("");
        textArea.setSingleLineTextArea(false);
        textArea.setGrowByContent(true);
        textArea.setFocusable(false);
        buttonOk = null;
        buttonOk = new Button("Ok");
        buttonOk.getSelectedStyle().setBgColor(0x000000);
        buttonOk.getSelectedStyle().setFgColor(0xffffff);
        buttonOk.getSelectedStyle().setBackgroundType(Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL, true);
        buttonOk.getSelectedStyle().setBackgroundGradientEndColor(0x000000);
        buttonOk.getSelectedStyle().setBackgroundGradientStartColor(0x0000ff);
        buttonOk.addActionListener(this);

        dialogMessage.addComponent(BorderLayout.CENTER, textArea);
        dialogMessage.addComponent(BorderLayout.SOUTH, buttonOk);
        textArea.setText(mess);
        //dialogMessage.show(50, 50, 50, 50, false, false);
        dialogMessage.show(90,90,10,10,true);
    }
    /**
     * Nhap ket qua tu server
     * @param type 
     */ 
    public void onStopConnect(String t){
        //textName.setText("84984819125"+ t);
        int r = -1;
        try {
            r = Integer.parseInt(t);
        } catch (NumberFormatException numberFormatException) {
            
        }
        String mess = "Lỗi kết nối!";
        if (r!=-1){
            switch (r){
                case Constant.ERROR_LOGIN_SUCCESS:
                    mess = "Đăng nhập thành công!";
                    System.out.println("Ket noi thanh cong");
                    parent.mainForm.mainFormInit();
                    return;
                case Constant.ERROR_LOGIN_NULL_USER_PASS:
                    mess = "User hoặc pass null!";
                    break;
                case Constant.ERROR_LOGIN_IVALID_USER_PASS:
                    mess = "User hoặc pass sai!";
                    break;
                case Constant.ERROR_LOGIN_ERROR:
                    mess = "Lỗi khi truy cập!";
                    break;
            }
        }
        new Toast(mess, Toast.TIME_SHORT);
    }

    public void actionPerformed(ActionEvent ae) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (ae.getComponent() == buttonOk)
        {
            parent.mainForm.mainFormInit();
        }
    }   
    
}
